/**
 * @(#)AppVisitedOrderController.java 2017年9月2日
 * 
 * Copyright 2000-2017 by ChinanetCenter Corporation.
 *
 * All rights reserved.
 *
 * This software is the confidential and proprietary information of
 * ChinanetCenter Corporation ("Confidential Information").  You
 * shall not disclose such Confidential Information and shall use
 * it only in accordance with the terms of the license agreement
 * you entered into with ChinanetCenter.
 * 
 */

package com.teamall.web.app.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.teamall.core.common.Page;
import com.teamall.framework.log.LogManager;
import com.teamall.framework.util.Utils;
import com.teamall.web.app.common.ResultFactory;
import com.teamall.web.app.vo.AppVisitedOrderVO;
import com.teamall.web.base.vo.ResResult;
import com.teamall.web.sjtj.db.po.CodeVisitedAidRankMonthPO;
import com.teamall.web.sjtj.db.po.CodeVisitedAidRankPO;
import com.teamall.web.sjtj.service.ICodeVisitedAidRankMonthService;
import com.teamall.web.sjtj.service.ICodeVisitedAidRankService;

/**
 * @author pan.xiaobo
 * @date 2017年9月2日
 * @version $Revision$
 */
@Controller
@RequestMapping("/app/visitedOrder")
public class AppVisitedOrderController {
	private static final LogManager logger = LogManager.getLogger(AppVisitedOrderController.class);

//	@Autowired
//	private ICodeVisitedOrderService codeVisitedOrderService;

	@Autowired
	private ICodeVisitedAidRankService codeVisitedAidRankService;

	@Autowired
	private ICodeVisitedAidRankMonthService codeVisitedAidRankMonthService;

	/**
	 * 3.9.	帮扶排名查询
	 * @param townId
	 * @param villageId
	 * @param year
	 * @param month
	 * @param page
	 * @param rows
	 * @return
	 */
	@RequestMapping("/qryVisitedOrder")
	@ResponseBody
	public ResResult qryVisitedOrder(
			@RequestParam(value = "townId", required = true) Integer townId,
			@RequestParam(value = "villageId", required = true) Integer villageId,
			@RequestParam(value = "year", required = true) String year,
			@RequestParam(value = "month", required = true) String month,
			@RequestParam(value = "page", required = true) Integer page,
			@RequestParam(value = "rows", required = true) Integer rows) {

		if (Utils.isEmpty(townId) || Utils.isEmpty(villageId) || Utils.isEmpty(year)
				|| Utils.isEmpty(month) || Utils.isEmpty(page) || Utils.isEmpty(rows)) {
			return ResultFactory.getRequestParamsError();
		}
		try {
			ResResult result = new ResResult();
			Page<AppVisitedOrderVO> resultPage = new Page<AppVisitedOrderVO>();
			resultPage.setPage(page);
			resultPage.setRows(rows);
			int sortNum = (page - 1) * rows + 1;
			if ("-1".equals(month)) {
				//按年
				CodeVisitedAidRankPO qryVisitedAidRankPO = new CodeVisitedAidRankPO();
				if (townId > 0) {
					qryVisitedAidRankPO.setTownId(townId);
				}
				if (villageId > 0) {
					qryVisitedAidRankPO.setVillageId(villageId);
				}
				qryVisitedAidRankPO.setVtime(year);
				Page<CodeVisitedAidRankPO> qryPage = new Page<CodeVisitedAidRankPO>();
				qryPage.setPage(page);
				qryPage.setRows(rows);
				codeVisitedAidRankService.selectByArgAndPage(qryVisitedAidRankPO, qryPage);

				BeanUtils.copyProperties(qryPage, resultPage);
				List<CodeVisitedAidRankPO> vList = qryPage.getResultList();
				List<AppVisitedOrderVO> resultList = new ArrayList<AppVisitedOrderVO>(rows);

				for (CodeVisitedAidRankPO vPO : vList) {
					AppVisitedOrderVO rVO = new AppVisitedOrderVO();
					BeanUtils.copyProperties(vPO, rVO);
					rVO.setVisitedCount(vPO.getVisitedCountSum());
					rVO.setSortNum(sortNum);
					sortNum++;
					resultList.add(rVO);
				}
				resultPage.setResultList(resultList);

			}
			else {
				//按月
				CodeVisitedAidRankMonthPO qryCodeVisitedAidRankMonthPO = new CodeVisitedAidRankMonthPO();
				if (townId > 0) {
					qryCodeVisitedAidRankMonthPO.setTownId(townId);
				}
				if (villageId > 0) {
					qryCodeVisitedAidRankMonthPO.setVillageId(villageId);
				}
				qryCodeVisitedAidRankMonthPO.setVtime(year);
				qryCodeVisitedAidRankMonthPO.setVmonth(month);
				Page<CodeVisitedAidRankMonthPO> qryPage = new Page<CodeVisitedAidRankMonthPO>();
				qryPage.setPage(page);
				qryPage.setRows(rows);
				codeVisitedAidRankMonthService.selectByArgAndPage(qryCodeVisitedAidRankMonthPO, qryPage);

				BeanUtils.copyProperties(qryPage, resultPage);
				List<CodeVisitedAidRankMonthPO> vList = qryPage.getResultList();
				List<AppVisitedOrderVO> resultList = new ArrayList<AppVisitedOrderVO>(rows);
				for (CodeVisitedAidRankMonthPO vPO : vList) {
					AppVisitedOrderVO rVO = new AppVisitedOrderVO();
					BeanUtils.copyProperties(vPO, rVO);
					rVO.setSortNum(sortNum);
					rVO.setVisitedCount(vPO.getVisitedCountSum());
					sortNum++;
					resultList.add(rVO);
				}
				resultPage.setResultList(resultList);
			}
			result.setRspData(resultPage);
			return result;
		}
		catch (Exception e) {
			logger.error(e);
			return ResultFactory.getExceptionResult();
		}

	}
}
